<?php

class Transaction extends CI_Model{

	function __construct()
    	{
        	parent::__construct();
    	}
	
	function store_userprofile($fName, $mName, $lName, $nName, $email, $msisdn, $otherCont, $birthdate, $street, $area, $town, $province, $postalCode, $gadgetId, $promoId)
	{
		$this->load->database();
		date_default_timezone_set("Asia/Manila");
		$current_timestamp = date("Y-m-d H:i:s", time());
		
		$data = array(
			'fName' => $fName,
			'mName' => $mName,
			'lName' => $lName,
			'nName' => $nName,
			'email' => $email,
			'msisdn' => $msisdn,
			'otherCont' => $otherCont,
			'birthdate' => $birthdate,
			'street' => $street,
			'area' => $area,
			'town' => $town,
			'province' => $province,
			'postalCode' => $postalCode,
			'gadgetId' => $gadgetId,
			'promoId' => $promoId,
			'commitmentPeriod' => $current_timestamp
		);

		try
		{
			$this->db->insert('Maitai_Subscriber', $data);
			$this->db->close();
		}
		catch(Exception $e)
		{
			return FALSE;
		}
	}
	
	function saveUserProfileToDB($fName, $mName, $lName, $nName, $email, $msisdn, $otherCont, $birthdate, $street, $area, $town, $province, $postalCode, $gadgetId, $promoId, $commitmentMonths)
	{
		$this->load->database();
		date_default_timezone_set("Asia/Manila");
		$current_timestamp = date("Y-m-d H:i:s", time());
		
		$data = array(
			'fName' => $fName,
			'mName' => $mName,
			'lName' => $lName,
			'nName' => $nName,
			'email' => $email,
			'msisdn' => $msisdn,
			'otherCont' => $otherCont,
			'birthdate' => $birthdate,
			'street' => $street,
			'area' => $area,
			'town' => $town,
			'province' => $province,
			'postalCode' => $postalCode,
			'gadgetId' => $gadgetId,
			'promoId' => $promoId,
			'commitmentPeriod' => $commitmentMonths,
			'registeredDate' => $current_timestamp
		);
		
		$tbl_subscriber = $this->config->item('table_subscriber');
		$query = $this->db->get_where($tbl_subscriber, array('msisdn' => $msisdn));
		
		// IS MERON NA IN TABLE?
		//if($q = $query->result_array()) {
		if($q = $query->result_array() && $msisdn!=0) {
			// NUMBER ALREADY IN DB
			$this->db->update($tbl_subscriber, $data, array('msisdn' => $msisdn));
			return TRUE;
		} else {
			// check if subsId already exists
			if($this->input->cookie('subsId')!=''){
				// exists, update nalang
				$this->db->update($tbl_subscriber, $data, array('subsId' => $this->input->cookie('subsId')));
			} else {
				// INSERT NEW DATA
				$this->db->insert($tbl_subscriber, $data);
				$subsId = $this->db->insert_id();
				$this->input->set_cookie(array('name'=>'subsId', 'value'=>$subsId, 'expire'=>0, 'path'=>'/'));
			}
			return TRUE;
		}
		return FALSE;
	}
	
	function saveStoreIdToDB($storeId)
	{
		$this->load->database();
		
		$data = array(
			'storeId' => $storeId
		);
		
		$tbl_subscriber = $this->config->item('table_subscriber');
		if(isset($_COOKIE['mobtel'])) $msisdn = $_COOKIE['mobtel'];
		$query = $this->db->get_where($tbl_subscriber, array('msisdn' => $msisdn));
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			// NUMBER ALREADY IN DB
			$this->db->update($tbl_subscriber, $data, array('msisdn' => $msisdn));
			return TRUE;
		} else {
			return FALSE;
		}
		return FALSE;
	}
	
    //public function retrieveUserProfileToDB($mobtel)
	public function retrieveUserProfileToDB($subsId)
	{
		$this->load->database();
		$tbl_subscriber = $this->config->item('table_subscriber');
		//$query = $this->db->get_where($tbl_subscriber, array('msisdn' => $mobtel));
		$query = $this->db->get_where($tbl_subscriber, array('subsId' => $subsId));
		return $query->result_array(); 
	}

	//public function updatePlanCode($mobtel, $plancode)
	public function updatePlanCode($subsId, $plancode)
	{
		$this->load->database();
		//if($mobtel!='' && $plancode!=''){
		if($subsId!='' && $plancode!=''){
			$data = array(
				'plancode' => $plancode
			);
			//$this->db->where('msisdn', $mobtel);
			$this->db->where('subsId', $subsId);
			$query = $this->db->update($this->config->item('table_subscriber'), $data); 
			return $query;
		}
		return false;
	}		
}
